package com.example.springboot_mybatis.controller;

import com.example.springboot_mybatis.MybatisUtils;
import com.example.springboot_mybatis.bean.Teacher;
import com.example.springboot_mybatis.mapper.StudentMapper;
import com.example.springboot_mybatis.mapper.TeacherMapper;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @RequestMapping("/list")
    public List<Teacher> teacher_findall(){
        SqlSession session = MybatisUtils.getSession();
        TeacherMapper mapper = session.getMapper(TeacherMapper.class);
        List<Teacher> stus = mapper.selectTeacher();
        session.close();
        return stus ;
    }

    @RequestMapping("/find")
    public Teacher teacher_find(String id){
        SqlSession session = MybatisUtils.getSession();
        TeacherMapper mapper = session.getMapper(TeacherMapper.class);
        Teacher stu = mapper.selectTeacherOne(id);
        session.close();
        return stu ;
    }

    @RequestMapping("/delete")
    public void teacher_delete( String id){
        SqlSession session = MybatisUtils.getSession();
        TeacherMapper mapper = session.getMapper(TeacherMapper.class);
        mapper.deleteTeacher(id);
        session.commit();
        session.close();
    }

    @RequestMapping("/add")
    public void teacher_add(String id,String name){
        SqlSession session = MybatisUtils.getSession();
        TeacherMapper mapper = session.getMapper(TeacherMapper.class);
        mapper.addTeacher(id,name);
        session.commit();
        session.close();
    }

    @RequestMapping("/edit")
    public void teacher_edit(String id,String name){
        SqlSession session = MybatisUtils.getSession();
        TeacherMapper mapper = session.getMapper(TeacherMapper.class);
        mapper.editTeacher(id,name);
        session.commit();
        session.close();
    }

}
